package com.tong.lib.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.tong.lib.mvp.IBaseView;
import com.tong.lib.utils.FixMenLeak;
import com.tong.lib.utils.StatusBarCompat;
import com.tong.lib.utils.permission.ScreenUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity implements IBaseView, StatusBarIml {

    private Unbinder mUnbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("BaseActivity", getClass().getSimpleName());
        if (ScreenUtils.isPortrait()) {
            ScreenUtils.adaptScreen4VerticalSlide(this, 360);
        } else {
            ScreenUtils.adaptScreen4HorizontalSlide(this, 360);
        }
        initFirst();
        if (isDataBinding()) {
            initDataBinding(getLayoutResID());
        } else {
            setContentView(getLayoutResID());
        }
        mUnbinder = ButterKnife.bind(this);
        if (isUseEventBus()) {
            EventBus.getDefault().register(this);
        }
        if (!isMvp()) {
            init(savedInstanceState);
            initEvent();
        }

    }

    protected void initDataBinding(int layoutRes) {

    }

    protected void initFirst() {
    }

    protected boolean isDataBinding() {
        return false;
    }


    @Override
    public void setStatusBarColor(int color) {
        StatusBarCompat.setColorNoTranslucent(this, getResources().getColor(color));
    }

    @Override
    public void setStatusBarIcon(boolean isDark) {
        if (isDark) {
            StatusBarCompat.darkMode(this, true);
        } else {
            StatusBarCompat.darkMode(this, false);
        }
    }

    protected abstract int getLayoutResID();

    protected abstract void init(Bundle savedInstanceState);

    protected abstract void initEvent();

    protected boolean isUseEventBus() {
        return false;
    }

    protected boolean isMvp() {
        return false;
    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        FixMenLeak.fixLeak(this);
        super.onDestroy();
        mUnbinder.unbind();
        if (isUseEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        /**
         * activity切换动画的一种方式
         */
//        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    @Override
    public void finish() {
        super.finish();
//        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void show(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
